<?php
/**
 * Author Yulin
 * Func   下单
 */
defined('IN_YuLin') || exit('NO PERMIT!');
include_once (ROOT_PATH.'module/lottery/lottery_play_conf.inc.php');
include_once (ROOT_PATH.'module/lottery/lottery_play_betting.inc.php');
UserLogin();

$bettingResInfo = array(
    'status' => 'success',
    'msg'    => '投注成功',
);

$lottInfo = $db->getrow('SELECT title, ename, sname, ptid, cate_id FROM ' . Table('lottery') . ' WHERE id = ' . $_POST['lott_id']);

// 获取到用户最基本的投注信息
$data = array(
    'play'    => $_POST['play'],    // 玩法
    'bet'     => $_POST['betting'], // 投注内容
    'lottery' => $_POST['lottery'], // 彩种简写  （k3, ssc
    'no'      => $_POST['no'],      // 期号
    'eo_money'=> $_POST['eo_money'],// 每注彩票投注金额
    'uid'     => $_uid,             // 用户
    'lott_id' => $_POST['lott_id'], // 大彩种id
    'platform'=> '2',
    'lottInfo'=> $lottInfo
);
// 解析用户投注内容
switch ($lottInfo['ptid']){
    case 'k3':
        $bettingResInfo = checkBettingPost($data, $bettingResInfo);
        if ($bettingResInfo['status'] == 'error'){
            break;
        }
        $orderData = betting_k3($data);
        // 检查账户余额， 是否足够一次性支付所有的投注
        if ($_user['money'] < $orderData['orderData']['main']['amount']){
            $bettingResInfo['status'] = 'error';
            $bettingResInfo['msg']    = '账户余额不足支付所有投注';
            break;
        }
        do_order($db, $orderData, $bettingResInfo);
        break;
    case 'syx5':
        break;
    default:
        break;
}
echo json_encode($bettingResInfo);


function checkBettingPost ($data, &$res){
    if (!$data['lott_id']){
        $res['status'] = 'error';
        $res['msg']    = '获取彩种失败，请重试';
    }
    if (!$data['no']){
        $res['status'] = 'error';
        $res['msg']    = '获取彩种期号失败，请重试';
    }
    if(!$data['eo_money']){
        $res['status'] = 'error';
        $res['msg']    = '获取投注金额失败，请重试';
    }
    if(!$data['bet']){
        $res['status'] = 'error';
        $res['msg']    = '获取投注信息失败，请重试';
    }
    if(!$data['play'] || !$data['lottery']){
        $res['status'] = 'error';
        $res['msg']    = '获取玩法信息失败，请重试';
    }
    return $res;
}

function do_order ($db, $data, &$res){
    if(!$data['orderData']['main'] || !$data['orderData']['item']) {
        $res['status'] = 'error';
        $res['msg']    = '网络繁忙,请刷新重试!';
        return;
    }
    // 记录主订单表信息
    $orderId = $db->add('lottery_order_main', $data['orderData']['main']);
    if (!$orderId){
        $res['status'] = 'error';
        $res['msg']    = '网络繁忙,请刷新重试!';
        return;
    }
    foreach($data['orderData']['item'] as $key => $val){
        $val['o_id']    = $orderId;
//         $orderid = $db->add('lottery_order_k3',$val);
        $execSql = 'INSERT INTO ' . Table('lottery_order_item') . ' SET ' . CreateUpdateSql($val);
        $orderid = $db->exec($execSql);
        if (!$orderid){
            $res['status'] = 'error';
            $res['msg']    = '网络繁忙,请刷新重试!';
            break;
        }
    }
    $tmp = [];
    $tmp['uid']     = $data['orderData']['main']['uid'];
    $tmp['tid']     = 'order_add';
    $tmp['lkey']    = $data['orderData']['main']['order_no'];
    $tmp['money']   = $data['orderData']['main']['amount'];
    $tmp['balance'] = -$data['orderData']['main']['amount'];
    $tmp['notes']   = '下注购买彩票：' . $data['lott_play'] . '-' . $data['orderData']['main']['code'];
    MoneyLog($tmp);
    // 代理返点相关，这里应该在下单后操作
    fanDian($data['orderData']['main'], $orderId);
    return $res;
}